package com.mzj.saas.mservice.smartlock.service.yaluete;

import com.alibaba.fastjson.JSON;
import com.mzj.saas.mservice.smartlock.vo.yaluete.YltCallBackVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 亚略特-事件回调
 * 
 * @author shun
 * @createDate: 2021年7月6日 上午9:55:50
 * @version: 1.0
 *
 */
@Service
public class SmartLockYltCallBackService {

	private static final Logger log = LoggerFactory.getLogger(SmartLockYltCallBackService.class);
	@Autowired
	private SmarkLockYltEventHandleService smarkLockYltEventHandleService;

	/**
	 * 亚略特-智能锁回调url
	 *
	 * @param vo
	 * @return
	 */
	public Map<String, Object> lockCallback(YltCallBackVO vo) {
		log.info("亚略特, 智能锁回调/事件url------vo={}", JSON.toJSONString(vo));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("desc", "成功");
		result.put("code", 0);
		try {

			if (StringUtils.isBlank(vo.getPushType())) {
				log.warn("参数错误");
				return result;
			}

			switch (vo.getPushType()) {
			case "17":// 开锁日志
				smarkLockYltEventHandleService.logEventHandle(vo);
				break;
			case "20":// 心跳日志
				smarkLockYltEventHandleService.lockOnlineCallBack(vo);
				break;
			case "21":// 告警日志
				smarkLockYltEventHandleService.lockAlarm(vo);
				break;
			default:
				log.warn("亚略特, 智能锁回调事件处理 = {}", vo.getPushType());
				break;
			}

			// 更新智能锁授权人数
			// updateOauthNumber(vo);
			return result;
		} catch (Exception e) {
			log.error("亚略特, 智能锁回调事件异常" + e.getMessage(), e);
		}
		return result;
	}
}
